package utils

import (
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
)

const (
	SuccessCode    = 1    // 成功
	ErrorCode      = 0    // 失败
	ErrorToken     = 2001 // token错误
	BadRequestCode = 400  // 请求错误
)

// 白名单
var WhiteList = []string{
	"auth/captcha",
	"auth/login",
	"auth/refreshtoken",
	"auth/create/menu",
	"users/info",
	"auth/logout",
	"files/upload",
	"files/delete",
	"config/lang",
	"config/data",
	"chatHistory/page",
	"chatGroup/page",
}

// SendJSONResponse 统一的 JSON 响应方法
func SendJSONResponse(c *gin.Context, statusCode int, data gin.H) {
	c.Header("Content-Type", "application/json")
	msg := ""
	if statusCode == SuccessCode {
		if data["msg"] == nil {
			msg = "success"
		} else {
			msg = data["msg"].(string)
		}
		response := map[string]interface{}{
			"data":       data,
			"code":       statusCode,
			"msg":        msg,
			"ttimestamp": time.Now().Unix(),
		}
		c.JSON(http.StatusOK, response)
	} else {
		if data["msg"] == nil {
			msg = "error"
		} else {
			msg = data["msg"].(string)
		}
		response := map[string]interface{}{
			"data":       data,
			"code":       statusCode,
			"msg":        msg,
			"ttimestamp": time.Now().Unix(),
		}
		c.JSON(http.StatusOK, response)
	}
}
